home *** CD-ROM | disk | FTP | other *** search
- Path: garlic.com!usenet
- From: Grant Robinson <grant@garlic.com>
- Newsgroups: comp.lang.c++
- Subject: MFC ASSERT_VALID
- Date: Fri, 29 Mar 1996 09:05:33 -0800
- Organization: South_Valley_Internet
- Message-ID: <315C185D.6B46@garlic.com>
- NNTP-Posting-Host: h.assign.garlic.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win16; I)
- CC: grant@garlic.com
-
- In the following program, why does the indicated ASSERT_VALID fail? I
- encountered this problem deriving my own container class from the MFC
- CArray template, which uses this method of memory allocation. The method
- used is not how I'd do things, but it's how Microsoft did, and I don't
- see why it shouldn't work.
-
- #include <afx.h>
-
- class CAge : public CObject
- {
- private:
- int m_years;
- public:
- CAge() { m_years = 0; }
- CAge(const int age) { m_years = age; }
- CAge(const CAge& a) { m_years = a.m_years; } // copy ctor
- const CAge& operator=(const CAge& a) // assignment
- { m_years = a.m_years; return *this; }
- BOOL operator==(CAge a)
- { return m_years == a.m_years; }
- #if defined(_DEBUG)
- void Dump(CDumpContext& dc) const
- {
- CObject::Dump( dc );
- dc << m_years;
- }
- void CAge::AssertValid() const
- {
- CObject::AssertValid();
- ASSERT(m_years >= 0);
- ASSERT(m_years < 105);
- }
- #endif // defined(_DEBUG)
- };
-
- void main()
- {
- { // This works...
- CAge* pCAge = new CAge[1];
- pCAge[0] = CAge(21);
- ASSERT(pCAge[0] == CAge(21)); // OK
- ASSERT_VALID(&pCAge[0]); // OK
- delete[] pCAge;
- }
- { // This doesn't...
- CAge* pCAge = (CAge*) new char[1 * sizeof(CAge)];
- pCAge[0] = CAge(21);
- ASSERT(pCAge[0] == CAge(21)); // OK
- ASSERT_VALID(&pCAge[0]); // AfxIsValidAddress fails:
- // "illegal vtable pointer"
- delete[] (char*)pCAge;
- }
- }
-